package com.tjbklx1.pattern.singleton.demo3;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 固定数量的单例模式
 * @author tjbklx1
 * @date 2018-10-10
 */
public class MultiEmperor {
	private static List<String> nameList = new ArrayList<String>();
	private static List<MultiEmperor> emperorList = new ArrayList<MultiEmperor>();
	private static final int maxNum=3;
	private static int num=0;
	
	private MultiEmperor() {
	}
	private MultiEmperor(String name){
		nameList.add(name);
	}
	static {
		for(int i=0;i<maxNum;i++){
			emperorList.add(new MultiEmperor("Great Emperor "+i));
		}
	}
	
	public static MultiEmperor getInstance(){
		Random random = new Random();
		num = random.nextInt(maxNum);
		return emperorList.get(num);
	}
	
	public void say(){
		System.out.println("I am Emperor :"+nameList.get(num));
	}
	
}
